<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>商品管理</title>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/table.css">
    <%@include file="../../include/commonFile.jsp" %>
</head>
<body>
<!--头部-->
<%@include file="../../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <form class="layui-form" action="${ctx}/mall/goods/list.do" id="myForm" method="post">
                <input type="hidden" name="pageNo" id="pageNo"/>
                <div class="f-search-bar">
                    <div class="search-container">
                        <ul class="search-form-content">
                            <li class="form-item-inline"><label class="search-form-lable">商品名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="name" autocomplete="off" class="layui-input"
                                           value="${goods.name}" placeholder="请输入查询商品名称">
                                </div>
                            </li>
                            <li class="form-item-inline">
                                <label class="search-form-lable">商品分类</label>
                                <div class="layui-input-inline">
                                    <select name="categoryId">
                                        <option value="">所有分类</option>
                                        <c:forEach var="parent" items="${categoryList}">
                                            <c:if test="${not empty parent.subCategories && fn:length(parent.subCategories) > 0}">
                                                <optgroup label="${parent.name}">
                                                    <c:forEach var="subject" items="${parent.subCategories}">
                                                        <option value="${subject.id}" ${subject.id == categoryId ? 'selected' : ''}>${subject.name}</option>
                                                    </c:forEach>
                                                </optgroup>
                                            </c:if>
                                        </c:forEach>
                                    </select>
                                </div>
                            </li>
                            <li class="form-item-inline">
                                <label class="search-form-lable">品牌</label>
                                <div class="layui-input-inline">
                                    <select name="brandId">
                                        <option value="">所有品牌</option>
                                        <c:forEach var="brand" items="${brandList}">
                                            <option value="${brand.key}" ${brand.key == brandId ? 'selected' : ''}>${brand.value}</option>
                                        </c:forEach>
                                    </select>
                                </div>
                            </li>
                        </ul>
                        <ul class="search-form-content">
                            <li class="form-item"><label class="search-form-lable">状&emsp;&emsp;态</label>
                                <div class="check-btn-inner">
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),'','#myForm')" ${empty goods.status ? 'class="active"' : ''}>全部</a>
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),4,'#myForm')" ${goods.status == 4 ? 'class="active"' : ''}>未开售</a>
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),1,'#myForm')" ${goods.status == 1 ? 'class="active"' : ''}>已上架</a>
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),2,'#myForm')" ${goods.status == 2 ? 'class="active"' : ''}>已下架</a>
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),3,'#myForm')" ${goods.status == 3 ? 'class="active"' : ''}>已售罄</a>
                                    <input type="hidden" name="status" value="${goods.status}"/>
                                </div>
                            </li>
                            <li class="form-item-inline">
                                <div class="sub-btns">
                                    <a class="layui-btn layui-btn-danger"
                                       href="javascript:submitFunction('#myForm')">查询</a>
                                    <a class="layui-btn layui-btn-normal"
                                       href="javascript:resetFunction('#myForm')">重置</a>
                                </div>
                            </li>
                        </ul>
                        <ul class="search-form-content">
                            <li class="form-item"><label class="search-form-lable">更新时间</label>
                                <div class="check-btn-inner" id="timeType">
                                    <a id="all" href="javascript:void(0);"
                                       onclick="setTimeType($(this),0,'#myForm')" ${empty input.timeType || input.timeType == 0 ? 'class="active"' : ''}>全部</a>
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),1,'#myForm')" ${input.timeType == 1 ? 'class="active"' : ''}>今天</a>
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),2,'#myForm')" ${input.timeType == 2 ? 'class="active"' : ''}>本周内</a>
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),3,'#myForm')" ${input.timeType == 3 ? 'class="active"' : ''}>本月内</a>
                                    <input type="hidden" name="timeType" value="${input.timeType}"/>
                                </div>
                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        <input class="layui-input" type="text" name="createStart"
                                               value="${input.createStart}" placeholder="开始日">
                                    </div>
                                    -
                                    <div class="layui-input-inline">
                                        <input class="layui-input" type="text" name="createEnd"
                                               value="${input.createEnd}" placeholder="截止日">
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </form>
            <div class="my-act-list-content">
                <ul class="num">
                    <a class="layui-btn layui-btn-danger layui-btn-sm" href="javascript:up()">上架</a>
                    <a class="layui-btn layui-btn-danger layui-btn-sm" href="javascript:down()">下架</a>
                    <a class="layui-btn layui-btn-danger layui-btn-sm" href="javascript:batchDelete()">批量删除</a>
                </ul>
                <div class="cl">
                    <table class="layui-table layui-form" id="myTable" lay-skin="line">
                        <colgroup>
                            <col width="50px">
                            <col width="70px">
                            <col>
                            <col width="8%">
                            <col width="8%">
                            <col width="8%">
                            <col width="8%">
                            <col width="8%">
                            <col width="150px">
                            <col width="100px">
                            <col>
                        </colgroup>
                        <thead>
                        <tr>
                            <th><input type="checkbox" id="chkAll" lay-skin="primary" lay-filter="chkAll"/></th>
                            <th>商品</th>
                            <th>价格</th>
                            <th>成本价</th>
                            <th>批发价</th>
                            <th>总库存</th>
                            <th>剩余库存</th>
                            <th>总销量</th>
                            <th>更新时间</th>
                            <th>商品状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach var="goods" items="${list}">
                            <tr>
                                <td><input type="checkbox" name="id" value="${goods.id}" lay-skin="primary" class="goods-chk" ${goods.status == 3 ? 'disabled' : ''} /></td>
                                <td>
                                    <img src="${goods.pic}" style="width: 50px; height: 50px">
                                </td>
                                <td>
                                    <div style="font-size: 13px;">
                                        <p style="line-height: 15px;" class="ellipsis-2">${goods.name}</p>
                                        <c:choose>
                                            <c:when test="${goods.type == 2}">
                                                <p class="red">${goods.price}积分</p>
                                            </c:when>
                                            <c:otherwise>
                                                <p class="red">￥${goods.price}</p>
                                            </c:otherwise>
                                        </c:choose>
                                    </div>
                                </td>
                                <td>
                                    <div>${goods.costPrice}</div>
                                </td>
                                <td>
                                    <div>${goods.tradePrice}</div>
                                </td>
                                <td>
                                    <div>${goods.stock}</div>
                                </td>
                                <td>
                                    <div>${goods.realStock}</div>
                                </td>
                                <td>
                                    <div>
                                        <c:if test="${goods.salesNum > 0}">
                                            <a class="red" target="_blank" href="${ctx}/mall/order/list.do?goodsId=${goods.id}&payStatus=1">${goods.salesNum}</a>
                                        </c:if>
                                        <c:if test="${goods.salesNum == 0}">
                                            -
                                        </c:if>
                                    </div>
                                </td>
                                <td>
                                    <div><fmt:formatDate value="${goods.updateDate}" pattern="yyyy-MM-dd HH:mm"/></div>
                                </td>
                                <td>
                                    <c:if test="${goods.status == 1}">
                                        <c:if test="${empty goods.openStartTime}">
                                            <div class="red">已上架</div>
                                        </c:if>
                                        <c:if test="${not empty goods.openStartTime}">
                                            <div class="green">未开售</div>
                                        </c:if>
                                    </c:if>
                                    <c:if test="${goods.status == 2}">
                                        <div class="dark">已下架</div>
                                    </c:if>
                                    <c:if test="${goods.status == 3}">
                                        <div class="gray">已售罄</div>
                                    </c:if>
                                </td>
                                <td>
                                    <div class="opts-btns tb-opts">
                                        <div class="comm-opts">
                                            <a class="qr-btn" href="javascript:txz.openQrDialog({
												title:'扫码二维码可预览分享',
												detailList:[{
													title:'详情二维码',
													url:'${goods.qrCodeUrl}'
												}]
											})">
                                                <i class="iconfont icon-qrcode btn-icon"></i> 二维码
                                            </a>
                                            <a href="javascript:copy('${goods.id}')">
                                                <i class="iconfont icon-copy btn-icon"></i> 复制
                                            </a>
                                            <a class="green" href="${ctx}/mall/goods/view.do?id=${goods.id}">
                                                <i class="iconfont icon-edit btn-icon"></i> 编辑
                                            </a>
                                            <a class="red"
                                               href="javascript:txz.deleteObject('确认要删除商品吗？', '${ctx}/mall/goods/deleteGoods.do?goodsId=${goods.id}')">
                                                <i class="iconfont icon-delete btn-icon"></i> 删除
                                            </a>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                    <div id="page_content" class="page-container"></div>
                </div>
            </div>
        </div>
        <!--底部-->
        <%@include file="../../include/footer.jsp" %>
    </section>
</div>

<script type="text/javascript" src="${ctx}/script/common/list.js"></script>
<script type="text/javascript" src="${ctx}/script/common/table_option.js"></script>
<script type="text/javascript">

    var analyzeT = null;
    $(function () {
        //加载分页
        loadPageNew({
            elem: 'page_content',
            totalCount: '${page.totalCount}',
            limit: '${page.limit}',
            page: '${page.page}'
        });

        layui.use(['laydate', 'form'], function () {
            var laydate = layui.laydate, form = layui.form;
            // 查询发布日期
            var start = {
                elem: 'input[name=createStart]'
            };

            var end = {
                elem: 'input[name=createEnd]'
            };
            laydate.render(start);
            laydate.render(end);

            form.on('checkbox(chkAll)', function (data) {
                var checked = data.elem.checked;
                if (checked) {
                    $(".goods-chk").each(function (index, ele) {
                        var disabled = $(ele).prop("disabled");
                        if (!disabled) {
                            $(ele).prop("checked", true);
                        }
                    });
                } else {
                    $(".goods-chk").each(function (index, ele) {
                        var disabled = $(ele).prop("disabled");
                        if (!disabled) {
                            $(ele).prop("checked", false);
                        }
                    });
                }
                form.render('checkbox');
            })
        });
    })
</script>
<script type="text/javascript">
    txz.initHeader({
        nav: [{
            name: '商品管理',
            curr: true
        }],
        info: '<span class="f12">共<b id="totalCount">${page.totalCount}</b>条记录</span>',
        btns: [{
            name: '发布商品',
            href: '${ctx}/mall/goods/view.do',
            icon: 'add'
        }]
    });

    // 上架
    function up() {
        var length = $(".goods-chk:checked").length;
        if (length == 0) {
            util.layerMsgError("请先选择商品");
            return false;
        }

        layer.confirm('确认要上架吗?', {
            icon: 3,
            title: '系统提示'
        }, function (index) {
            var _idArray = new Array();
            $(".goods-chk:checked").each(function (index, ele) {
                _idArray.push($(ele).val());
            });
            var ids = _idArray.join(",");

            var url = "${ctx}/mall/goods/upOrDown.do";
            //loading层
            var loadIndex = layer.load(1, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });
            $.post(url, {"goodsIds": ids, "status": 1}, function (res) {
                top.layer.close(loadIndex);
                if (res.success) {
                    util.layerMsgSuccess("操作成功", function () {
                        location.reload();
                    });
                } else {
                    util.layerMsgError("操作异常");
                }
            });
            top.layer.close(index);
        });
    }

    // 下架
    function down() {
        var length = $(".goods-chk:checked").length;
        if (length == 0) {
            util.layerMsgError("请先选择商品");
            return false;
        }

        layer.confirm('确认要下架吗?', {
            icon: 3,
            title: '系统提示'
        }, function (index) {
            var _idArray = new Array();
            $(".goods-chk:checked").each(function (index, ele) {
                _idArray.push($(ele).val());
            });
            var ids = _idArray.join(",");

            var url = "${ctx}/mall/goods/upOrDown.do";
            //loading层
            var loadIndex = layer.load(1, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });
            $.post(url, {"goodsIds": ids, "status": 2}, function (res) {
                top.layer.close(loadIndex);
                if (res.success) {
                    util.layerMsgSuccess("操作成功" + res.description, function () {
                        location.reload();
                    });
                } else {
                    util.layerMsgError("操作异常");
                }
            });
            top.layer.close(index);
        });
    }

    // 批量删除
    function batchDelete() {
        var goods = $(".goods-chk:checked");
        var length = goods.length;
        if (length == 0) {
            util.layerMsgError("请先选择商品");
            return false;
        }
        var _idArray = new Array();
        goods.each(function (index, ele) {
            _idArray.push($(ele).val());
        });
        var ids = _idArray.join(",");

        layer.confirm('确认要将选中的全部删除吗?', {
            icon: 3,
            title: '警告'
        }, function (index) {
            //loading层
            var loadIndex = layer.load(1, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });
            var url = "${ctx}/mall/goods/batchDelete.do";
            $.post(url, {ids: ids}, function (res) {
                top.layer.close(loadIndex);
                if (res.success) {
                    util.layerMsgSuccess("批量删除成功", function () {
                        location.reload();
                    });
                } else {
                    util.layerMsgError(res.description)
                }
            });
            top.layer.close(index);
        });
    }

    // 复制
    function copy(goodsId) {
        layer.confirm('确认要复制吗?', {
            icon: 3,
            title: '系统提示'
        }, function (index) {
            //loading层
            var loadIndex = layer.load(1, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });
            var url = "${ctx}/mall/goods/copyGoods.do";
            $.post(url, {"goodsId": goodsId}, function (res) {
                top.layer.close(loadIndex);
                if (res.success) {
                    util.layerMsgSuccess("复制成功", function () {
                        location.reload();
                    });
                } else {
                    util.layerMsgError("复制失败");
                }
            });
            top.layer.close(index);
        });
    }
</script>
</body>
</html>